'use strict';
const dayjs = require("dayjs");

module.exports = app => {
    const { STRING, INTEGER, DATE, TEXT } = app.Sequelize;
    const Conversation = app.model.define(
        'Conversation',
        {
            id: { type: INTEGER, primaryKey: true, autoIncrement: true },
            user_id: { type: INTEGER, allowNull: false },
            title: { type: STRING(255), allowNull: false },
            model: { type: STRING(50), allowNull: false, defaultValue: 'gpt-3.5-turbo' },
            is_archived: { type: INTEGER, defaultValue: 0 }, // 0: 未归档, 1: 已归档
            type: {
                type: STRING,
                defaultValue: 'text'
            },
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            update_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'conversation',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    Conversation.associate = function() {
        app.model.Conversation.belongsTo(app.model.User, { foreignKey: 'user_id' });
    };

    return Conversation;
};

// MySQL建表语句
// CREATE TABLE `conversation` (
//   `id` int(11) NOT NULL AUTO_INCREMENT,
//   `user_id` int(11) NOT NULL COMMENT '用户ID',
//   `title` varchar(255) NOT NULL COMMENT '会话标题',
//   `model` varchar(50) NOT NULL DEFAULT 'gpt-3.5-turbo' COMMENT 'AI模型',
//   `is_archived` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否归档 0:未归档 1:已归档',
//   `create_time` datetime DEFAULT NULL COMMENT '创建时间',
//   `update_time` datetime DEFAULT NULL COMMENT '更新时间',
//   PRIMARY KEY (`id`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI对话会话表';
